import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisDemoString {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
            //test2(jedis);
            //test3(jedis);
            test4(jedis);
        }
    }

    private static void test4(Jedis jedis) {
        System.out.println("数字操作");
        jedis.flushAll();
        jedis.set("key1", "10");
        long result = jedis.incr("key1");
        System.out.println(result);
        String result2 = jedis.get("key1");
        System.out.println(result2);
        result = jedis.decr("key1");
        System.out.println(result);
        result2 = jedis.get("key1");
        System.out.println(result2);
    }

    private static void test3(Jedis jedis) {
        System.out.println("append");
        jedis.flushAll();
        jedis.set("key1","abc");
        String result = jedis.get("key1");
        System.out.println(result);
        jedis.append("key1", "efg");
        result = jedis.get("key1");
        System.out.println(result);

    }

    private static void test2(Jedis jedis) {
        System.out.println("getrange 和 setrange 的操作");
        jedis.flushAll();
        jedis.set("key1", "abcdefgh");
        String result = jedis.getrange("key1", 2, 5);
        System.out.println(result);
        jedis.setrange("key1", 2, "bcd");
        result = jedis.getrange("key1", 2, 5);
        System.out.println(result);
    }


    private static void test1(Jedis jedis) {
        System.out.println("mset 和 mget 的操作");
        jedis.flushAll();
        jedis.mset("key1", "111", "key2", "222", "key3", "333");
        List<String> list = jedis.mget("key1", "key2", "key3", "key22");//返回的value和此处的key一一对应，若key是没有的，则返回null
        System.out.println(list);
    }
}
